package com.maximus.server.nio.mainsubreactor;

import com.maximus.server.nio.ReadAndWrite;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class WorkHandler implements Runnable {
    private static final Logger logger = LogManager.getLogger(WorkHandler.class);

    private SocketChannel socketChannel;

    public WorkHandler(SocketChannel socketChannel) {
        this.socketChannel = socketChannel;
    }

    @Override
    public void run() {
        try {
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            ReadAndWrite.readAndWrite(socketChannel, byteBuffer);
        } catch (IOException e) {
            try {
                logger.info(socketChannel.getRemoteAddress() + "离线了...");
                socketChannel.close();
            }
            catch (IOException ioException) {
                logger.error(ioException);
            }
        }
    }
}
